/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-10-14     RT-Thread    aht20 sample
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "drv_aht20.h"

#define DBG_TAG "aht20.sample"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#define AHT20_THREAD_PRIORITY     15
#define AHT20_THREAD_STACK_SIZE   1024
#define AHT20_THREAD_TIMESLICE    5

static aht20_device_t aht20_dev = RT_NULL;

static int aht20_read(int argc, char **argv)
{
    float temperature = 0.0;
    float humidity = 0.0;
    rt_err_t result;

    if (aht20_dev == RT_NULL)
    {
        rt_kprintf("AHT20 device not initialized!\n");
        return -RT_ERROR;
    }

    result = aht20_read_temperature_humidity(aht20_dev, &temperature, &humidity);
    if (result == RT_EOK)
    {
        rt_kprintf("Temperature: %.2f C\n", temperature);
        rt_kprintf("Humidity: %.2f %%\n", humidity);
    }
    else
    {
        rt_kprintf("Read AHT20 failed!\n");
        return -RT_ERROR;
    }

    return RT_EOK;
}

int aht20_sample_init(void)
{
    aht20_dev = aht20_init(AHT20_I2C_BUS_NAME);
    if (aht20_dev == RT_NULL)
    {
        LOG_E("AHT20 init failed");
        return -RT_ERROR;
    }

    LOG_I("AHT20 initialized, use 'aht20_read' command to get data");

    return RT_EOK;
}


MSH_CMD_EXPORT(aht20_read, Read AHT20 temperature and humidity);

